import {
  productDetail
} from '@/util/api/product'
// import { isMobile } from '@/common/utils/common'
// import { getUser,createOrder } from "@/api/user/index.js";
import {
  userInfo
} from "@/util/api/user";
import {
  isMobile
} from "@/util/validator.js";
import {
  pageUrl,
  appId
} from "@/util/api.js";
export default {
  data() {
    return {
      id: '',
      detailInfo: {},
      customerData: {
        mailingAddress: '',
        contactPerson: '',
        contactNumber: ''
      },
      check: false,
      openId: '',
      userId: ''
    }
  },
  onLoad(e) {
    this.id = e.id
    this.getDetail()
    this.getUser()
    let that = this;

    uni.getSystemInfo({
      success: function (res) {
        console.log("wx.getSystemInfo")
        console.log(res)
        if (res.platform == "ios") {
          that.showOpen = false
        }
      }
    })
  },
  onShow() {
    this.openId = uni.getStorageSync("openId")
  },
  methods: {
    getUser() {
      userInfo().then((res) => {
        this.userId = res.result.id;
      });
    },
    getDetail() {
      productDetail(this.id).then(res => {
        if (res.code === 200) {
          this.detailInfo = res.result
        } else {
          uni.showToast({
            title: res.message,
            duration: 2000,
            icon: "none"
          });
        }
      })
    },
    // checkboxChange() {
    //   this.check = !this.check
    // },
    pay() {
      let customerData = [{
          value: this.customerData.mailingAddress,
          text: '收件地址'
        },
        {
          value: this.customerData.contactPerson,
          text: '收件人'
        },
        {
          value: this.customerData.contactNumber,
          text: '收件电话'
        }
      ]
      for (let i = 0; i < customerData.length; i++) {
        let obj = customerData[i]
        if (!obj.value) {
          uni.showToast({
            title: `请填写${obj.text}`,
            duration: 2000,
            icon: "none",
          });
          return
        }
      }
      if (!isMobile(this.customerData.contactNumber)) {
        uni.showToast({
          title: "请输入正确的手机号码",
          duration: 2000,
          icon: "none",
        });
        return
      }
      this.immediatelyPay()
    },
    immediatelyPay() {
      uni.webView.getEnv((res) => {
        window.location.href =
          `https://open.weixin.qq.com/connect/oauth2/authorize?appid=` +
          appId +
          `&redirect_uri=` +
          encodeURIComponent(
            pageUrl +
            `/app/payNew.html?payType=` +
            '090' +
            "&userId=" + this.userId +
            "&productId=" + this.id +
            "&contactNumber=" + this.customerData.contactNumber +
            "&contactPerson=" + this.customerData.contactPerson +
            "&mailingAddress=" + this.customerData.mailingAddress +
            "&redirectUrl=" +
            (window.location.protocol + "//" + window.location.host) +
            "/pages/mime/productOrder"
          ) +
          `&response_type=code&scope=snsapi_base`;
      })
    },
  }
}